package com.example.demo.news;

import com.example.demo.news.bean.NewsBean;
import com.example.demo.news.bean.NewsType;
import com.example.demo.news.service.NewsService;
import com.example.demo.news.service.NewsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;
import java.time.LocalDateTime;

@Controller
@RequestMapping("Admin/news")
public class NewsController {

    private static final  String rootName = "/news/";

    @Value("${config.page.size}")
    private int pageSize;

    @Autowired
    private NewsTypeService newsTypeService;

    @Autowired
    private NewsService newsService;

    /**
     * 新闻类型列表
     * @param page
     * @param model
     * @return
     */
    @RequestMapping(value = "newstypelist")
    public String getNewsTypeList(@RequestParam(name = "page",required = false) String page, Model model){
            PageRequest pageRequest =null;
            if (null==page){
                pageRequest = new PageRequest(0,pageSize);
            }else{
            pageRequest = new PageRequest(Integer.valueOf(page)-1,pageSize);
        }
            model.addAttribute("newstypelist",newsTypeService.getAll(pageRequest));
        return rootName+"newstypelist";
    }

    /**
     * 添加新闻类型
     * @param model
     * @return
     */
    @RequestMapping(value = "addnewstype",method = RequestMethod.GET)
    public String addNewsType(Model model){
        model.addAttribute("entity",new NewsType());
        return rootName+"addnewstype";
    }

    /**
     * 修改新闻类型
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value ="editnewstype/{id}")
    public String editNewsType(@PathVariable int id, Model model){
        model.addAttribute("entity",newsTypeService.getById(id));
        return rootName+"addnewstype";
    }

    /**
     * 保存新闻类型
     * @param newsType
     * @param result
     * @return
     */
    @RequestMapping(value = "savenewstype",method = RequestMethod.POST)
    public String saveNewsType(@Valid NewsType newsType, BindingResult result){
        if (result.hasErrors())
        {
            return rootName+"addnewstype";
        }
        newsTypeService.save(newsType);
        return "redirect:/Admin/news/newstypelist";
    }

    /**
     * 删除新闻类型
     * @param id
     * @return
     */
    @RequestMapping(value = "delnewstype/{id}")
    public String delNewsType(@PathVariable int id){
        newsTypeService.delEntity(id);
        return "redirect:/Admin/news/newstypelist";
    }

    /**
     * 新闻列表
     * @param page
     * @param model
     * @return
     */
    @RequestMapping(value = "newslist")
    public String getNewsList(@RequestParam(name = "page",required = false) String page, Model model){
        PageRequest pageRequest =null;
        if (null==page){
            pageRequest = new PageRequest(0,pageSize);
        }else{
            pageRequest = new PageRequest(Integer.valueOf(page)-1,pageSize);
        }
        model.addAttribute("newslist",newsService.getAll(pageRequest));
        return rootName+"newslist";
    }

    /**
     * 删除新闻
     * @param id
     * @return
     */
    @RequestMapping(value = "delnews/{id}")
    public String delNews(@PathVariable int id){
        newsService.delEntity(id);
        return "redirect:/Admin/news/newslist";
    }

    /**
     * 添加新闻
     * @param model
     * @return
     */
    @RequestMapping(value = "addnews")
    public String addNews(Model model){
        model.addAttribute("entity",new NewsBean());
        model.addAttribute("typelist",newsTypeService.getAll());
        return rootName+"addnews";
    }

    /**
     * 保存新闻
     * @param newsBean
     * @param result
     * @return
     */
    @RequestMapping(value = "savenews")
    public String saveNews(@Valid NewsBean newsBean,BindingResult result){
        if (result.hasErrors()){
            return rootName+"addnews";
        }
        newsService.save(newsBean);
        return "redirect:/Admin/news/newslist";
    }

    /**
     * 编辑新闻
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "editnews/{id}")
    public String editNews(@PathVariable int id,Model model){
        NewsBean newsBean = null;
        newsBean = newsService.getById(id);
        if (newsBean==null) {
         newsBean = new NewsBean();
         newsBean.setId(id);
        }
        model.addAttribute("entity",newsBean);
        model.addAttribute("typelist",newsTypeService.getAll());
        return rootName+"addnews";
    }


    @RequestMapping(value = "newslist/{id}")
    public String newslistByType(@PathVariable int id,@RequestParam(name = "page",required = false) String page, Model model){
        PageRequest pageRequest =null;
        if (null==page){
            pageRequest = new PageRequest(0,pageSize);
        }else{
            pageRequest = new PageRequest(Integer.valueOf(page)-1,pageSize);
        }

        model.addAttribute("newslist",newsService.getNewsByType(pageRequest,newsTypeService.getById(id)));
        return rootName+"newslist";
    }


}
